<template>
  <div class="swiperconfig">
    <div class="swiperleft">
      <div class="configcenter">
        <div class="title">默认图</div>
        <div class="baseconfig">基础配置</div>
      </div>
    </div>
    <div class="swipercenter">
      <span>轮播图默认图片配置：</span>
      <el-upload
        class="avatar-uploader"
        action="https://api.xstarlife.com/systemapp/upload/singleFileUpload"
        :show-file-list="false"
        :headers="{ uploadScene: 'avatar' }"
        :on-success="swiperImgsussess"
        :before-upload="beforeAvatarUpload"
      >
        <img
          v-if="bannerDefaultPic"
          :src="bannerDefaultPic"
          class="avatar"
          @mouseover="picMessage"
        />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </div>
    <div class="swiperright">
      <div class="save" @click="save">保存</div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    systemConfigDefaultPic: {
      type: Object,
      required: false
    }
  },
  data() {
    return {
      configType: "system_config_default_icon",
      bannerDefaultPic: "",
      id: "",
      messageTip: true
    };
  },
  methods: {
    swiperImgsussess(res, file) {
      this.bannerDefaultPic = res.data;
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isLt2M;
    },
    async save() {
      if (!this.bannerDefaultPic) {
        this.$message.error("请先添加图片");
        return false;
      }
      const data = {
        systemConfigDefaultPic: {
          configType: "system_config_default_icon",
          bannerDefaultPic: this.bannerDefaultPic
        }
      };
      if (this.id) {
        data.systemConfigDefaultPic.id = this.id;
      }
      await this.$http({
        url: "/hsssystemadmin/config/system/updateSystemConfig",
        method: "POST",
        data
      })
        .then(res => {
          this.$message.success("轮播图默认图片配置成功");
          this.$emit("research");
        })
        .catch(err => {
          this.$message.error("轮播图默认图片配置失败");
        });
    },
    picMessage() {
      if (this.messageTip) {
        this.messageTip = false;
        setTimeout(() => {
          this.$message.success("点击更换图片");
          this.messageTip = true;
        }, 3000);
      }
    }
  },
  watch: {
    systemConfigDefaultPic(newvalue) {
      if (newvalue) {
        this.configType = newvalue.configType;
        this.bannerDefaultPic = newvalue.bannerDefaultPic;
        this.id = newvalue.id;
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.swiperconfig {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
  font-size: 12px;
  .swiperleft {
    width: 150px;
    min-height: 150px;
    background-color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
    .configcenter {
      width: 100px;
      height: 70px;
      .title {
        width: 100%;
        height: 35px;
        background-color: #f56a23;
        font-size: 22px;
        color: #fff;
        line-height: 35px;
        text-align: center;
      }
      .baseconfig {
        width: 100%;
        height: 35px;
        font-size: 22px;
        line-height: 35px;
        text-align: center;
        color: #aaa;
      }
    }
  }
  .swiperright {
    width: 150px;
    min-height: 150px;
    background-color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
    .save {
      width: 70px;
      height: 40px;
      font-size: 22px;
      text-align: center;
      color: #fff;
      background-color: #00cb75;
      line-height: 40px;
    }
  }
  .swipercenter {
    flex: 1;
    min-height: 150px;
    background-color: #fff;
    margin-left: 10px;
    margin-right: 10px;
    display: flex;
    align-items: center;
    padding: 20px;
    font-size: 12px;
    /deep/ .avatar-uploader .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
    /deep/ .avatar-uploader .el-upload:hover {
      border-color: #409eff;
    }
    /deep/ .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 80px;
      height: 80px;
      line-height: 80px;
      text-align: center;
    }
    /deep/ .avatar {
      width: 80px;
      height: 80px;
      display: block;
    }
  }
}
</style>
